package com.hex.ds.hdrs.period.task.controller;

import com.hex.bigdata.hbda.annotation.AnonymousAccess;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.common.page.PageData;
import com.hex.ds.hdrs.engine.service.QueueService;
import com.hex.ds.hdrs.period.job.po.PeriodJob;
import com.hex.ds.hdrs.period.task.dto.PeriodTaskDto;
import com.hex.ds.hdrs.period.task.po.PeriodTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "队列管理")
@RequestMapping("/api/queue")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class QueueController {

    private final QueueService queueService;

    /*
     * @Method: getQueueData <br>
     * @Param: [jobCode] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：获取队列中所有数据，用于排查问题（非登录请求）<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/28 10:54 <br>
     * @Version： V2.0.2<br>
     */
    @Log("获取队列中所有数据")
    @ApiOperation("获取队列中所有数据")
    @AnonymousAccess
    @GetMapping(value = "/getQueueData")
    public ResponseEntity<Object> getQueueData(@RequestParam("jobCode") String jobCode) {
        return new ResponseEntity(queueService.getQueue(jobCode), HttpStatus.OK);
    }

    @ApiOperation("查询待执行队列列表")
    @GetMapping(value = "/qryToDoQueue")
    public ResponseEntity<Object> qryToDoQueue(PeriodTask periodTask, Page page) {
        PageData pageData = new PageData(queueService.qryToDoQueue(periodTask, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("待执行队列立即执行")
    @ApiOperation("待执行队列立即执行")
    @PostMapping(value = "/toDoImplement")
    public ResponseEntity<Object> toDoImplement(@RequestBody PeriodTask task) {
        this.queueService.toDoImplement(task.getJobCode(), task.getBizDate());
        return new ResponseEntity(null, HttpStatus.OK);
    }

    @Log("待执行队列移除")
    @ApiOperation("待执行队列移除")
    @PostMapping(value = "/toDoRemove")
    public ResponseEntity<Object> toDoRemove(@RequestBody PeriodTask task) {
        this.queueService.toDoRemove(task.getJobCode(), task.getBizDate());
        return new ResponseEntity(null, HttpStatus.OK);
    }

    /**
     * @Method: toDoListRemove
     * @Param: [taskList]
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description: 待执行队列批量删除
     * @Author: ji.tang
     * @Date: 2022/3/21 14:50
     * @Version: V1.0
     */
    @Log("待执行队列批量移除")
    @ApiOperation("待执行队列批量移除")
    @PostMapping(value = "/toDoListRemove")
    public ResponseEntity<Object> toDoListRemove(@RequestBody List<PeriodTask> taskList) {
        this.queueService.toDoListRemove(taskList);
        return new ResponseEntity(null, HttpStatus.OK);
    }

    @Log("查询可执行队列列表")
    @ApiOperation("查询可执行队列列表")
    @GetMapping(value = "/qryToRunQueue")
    public ResponseEntity<Object> qryToRunQueue(PeriodTask periodTask, Page page) {
        PageData pageData = new PageData(this.queueService.qryToRunQueue(periodTask, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("根据app查询可执行队列")
    @ApiOperation("根据app查询可执行队列")
    @GetMapping(value = "/qryAppToRunQueue")
    public ResponseEntity<Object> qryAppToRunQueue(PeriodTask periodTask, Page page) {
        PageData pageData = new PageData(this.queueService.qryAppToRunQueue(periodTask, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("根据app查询可执行延迟队列")
    @ApiOperation("根据app查询可执行延迟队列")
    @GetMapping(value = "/qryAppToRunDelayQueue")
    public ResponseEntity<Object> qryAppToRunDelayQueue(PeriodTask periodTask, Page page) {
        PageData pageData = new PageData(this.queueService.qryAppToRunDelayQueue(periodTask, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("查询可执行队列所有app")
    @ApiOperation("查询可执行队列所有app")
    @GetMapping(value = "/qryToRunApp")
    public ResponseEntity<Object> qryToRunQueue() {
        List<PeriodTask> list = this.queueService.qryToRunApp();
        return new ResponseEntity(list, HttpStatus.OK);
    }

    @Log("可执行队列立即执行")
    @ApiOperation("可执行队列立即执行")
    @PostMapping(value = "/toRunImplement")
    public ResponseEntity<Object> toRunImplement(@RequestBody PeriodTask periodTask) {
        this.queueService.toRunImplement(periodTask.getTaskCode(), periodTask.getBizDate());
        return new ResponseEntity(null, HttpStatus.OK);
    }

    @Log("可执行队列移除")
    @ApiOperation("可执行队列移除")
    @PostMapping(value = "/toRunRemove")
    public ResponseEntity<Object> toRunRemove(@RequestBody PeriodTask periodTask) {
        this.queueService.toRunRemove(periodTask);
        return new ResponseEntity(null, HttpStatus.OK);
    }

    /**
     * @Method: toRunListRemove
     * @Param: [periodTaskList]
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description: 可执行队列批量移除
     * @Author: ji.tang
     * @Date: 2022/7/5 14:07
     * @Version: V1.0
     */
    @Log("可执行队列批量移除")
    @ApiOperation("可执行队列批量移除")
    @PostMapping(value = "/toRunListRemove")
    public ResponseEntity<Object> toRunListRemove(@RequestBody List<PeriodTask> periodTaskList) {
        this.queueService.toRunListRemove(periodTaskList);
        return new ResponseEntity(null, HttpStatus.OK);
    }

    @Log("可执行队列置顶")
    @ApiOperation("可执行队列置顶")
    @PostMapping(value = "/toRunTop")
    public ResponseEntity<Object> toRunTop(@RequestBody PeriodTask periodTask) {
        this.queueService.toRunTop(periodTask.getJobCode(), periodTask.getBizDate());
        return new ResponseEntity(null, HttpStatus.OK);
    }

    @Log("可执行队列升序/降序")
    @ApiOperation("可执行队列升序/降序")
    @PostMapping(value = "/toRunSort")
    public ResponseEntity<Object> toRunSort(@RequestBody PeriodTaskDto periodTask) {
        this.queueService.toRunSort(periodTask.getJobCode(), periodTask.getBizDate(),
                periodTask.getSortType(), periodTask.getNum());
        return new ResponseEntity(null, HttpStatus.OK);
    }

    @Log("查询正在执行队列列表")
    @ApiOperation("查询正在执行队列列表")
    @GetMapping(value = "/qryRunningQueue")
    public ResponseEntity<Object> qryRunningQueue(PeriodTask periodTask, Page page) {
        PageData pageData = new PageData(queueService.qryRunningQueue(periodTask, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }


    @Log("正在执行队列移除")
    @ApiOperation("正在执行队列移除")
    @PostMapping(value = "/runningRemove")
    public ResponseEntity<Object> runningRemove(@RequestBody PeriodTask periodTask) {
        this.queueService.runningRemove(periodTask.getTaskCode(), periodTask.getBizDate());
        return new ResponseEntity(null, HttpStatus.OK);
    }


    @Log("正在执行队列移除")
    @ApiOperation("正在执行队列移除")
    @PostMapping(value = "/runningKillTaskProcess")
    public ResponseEntity<Object> runningKillTaskProcess(@RequestBody PeriodTask periodTask) {
        this.queueService.runningRemove(periodTask.getTaskCode(), periodTask.getBizDate());
        return new ResponseEntity(null, HttpStatus.OK);
    }

    /**
     * @Method: runningKillTaskListProcess
     * @Param: [periodTaskList]
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description: 正在执行队列批量移除
     * @Author: ji.tang
     * @Date: 2022/7/5 14:14
     * @Version: V1.0
     */
    @Log("正在执行队列批量移除")
    @ApiOperation("正在执行队列批量移除")
    @PostMapping(value = "/runningKillTaskListProcess")
    public ResponseEntity<Object> runningKillTaskListProcess(@RequestBody List<PeriodTask> periodTaskList) {
        this.queueService.runningKillTaskListProcess(periodTaskList);
        return new ResponseEntity(null, HttpStatus.OK);
    }

    @Log("查询最新完成作业")
    @ApiOperation("查询最新完成作业")
    @GetMapping(value = "/qryLatestJob")
    public ResponseEntity<Object> qryLatestJob(PeriodJob periodJob, Page page) {
        PageData pageData = new PageData(queueService.qryLatestJob(periodJob, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("查询最新完成任务")
    @ApiOperation("查询最新完成任务")
    @GetMapping(value = "/qryLatestTask")
    public ResponseEntity<Object> qryLatestTask(PeriodTask periodTask, Page page) {
        PageData pageData = new PageData(queueService.qryLatestTask(periodTask, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @ApiOperation("查询待删除作业")
    @GetMapping(value = "/qryRemoveJob")
    public ResponseEntity<Object> qryRemoveTask(PeriodTask periodTask, Page page) {
        PageData pageData = new PageData(this.queueService.qryRemoveTask(periodTask, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }
}
